package com.iweb.order.Dao;

import com.iweb.order.model.Order;
import com.iweb.shopping.model.Address_sh;
import com.iweb.shopping.model.Goods;
import com.iweb.util.JdbcUtil;
import com.iweb.util.PageModel;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class OrderDaoImpl implements OrderDao{
    @Override
    public List<Order> selectByPage(PageModel pageModel, Integer userid) {
        List<Order> orders = new ArrayList<>();
        String likeValue = pageModel.getLikeValue();
        StringBuffer sql =new StringBuffer( "select o.orderid,tg.goodsname,tg.goodsprice,o.buyerid,o.salerid,date_format(o.createtime,'%Y-%m-%d %H:%i:%s') createtime,o.orderstatus,ta.address " +
                "from tb_order o,tb_goods tg,tb_address ta where o.goodsid=tg.goodsid and o.addressid=ta.addressid and (o.buyerid=? or o.salerid=?) and (tg.goodsname like ? or tg.goodsprice like ?)");
        int recordCount = JdbcUtil.selectBySql(sql.toString(),userid,userid,"%"+likeValue+"%","%"+likeValue+"%").size();
        pageModel.setRecordCount(recordCount);
        sql.append(" order by orderid desc limit ?,? ");
        int startNo = pageModel.getPageSize()*(pageModel.getPageNo()-1);
        List<Map<String,Object>> list = JdbcUtil.selectBySql(sql.toString(),userid,userid,"%"+likeValue+"%","%"+likeValue+"%",startNo,pageModel.getPageSize());
        for (int i = 0; i < list.size(); i++) {
            Map<String,Object> map = list.get(i);
            Integer orderid = (Integer) map.get("orderid");
            String goodsname= (String) map.get("goodsname");
            String goodsprice= (String) map.get("goodsprice");
            Integer buyerid = (Integer) map.get("buyerid");
            Integer salerid = (Integer) map.get("salerid");
            String createtime = (String) map.get("createtime");
            Integer orderstatus = (Integer) map.get("orderstatus");
            String address = (String) map.get("address");
            orders.add(new Order(orderid,goodsname,goodsprice,buyerid,salerid,createtime,orderstatus,address));
        }
        return orders;
    }

    @Override
    public int deleteOne(Integer orderid) {
        String sql = "delete from tb_order where orderid = ?";
        return JdbcUtil.editBySql(sql,orderid);
    }

    @Override
    public Goods selectByGoodsid(Integer goodsid) {
        String sql ="select goodsid,userid from tb_goods where goodsid = ?";
        List<Map<String,Object>> list = JdbcUtil.selectBySql(sql,goodsid);
        Map<String,Object> map = list.get(0);
        Integer userid = (Integer) map.get("userid");
        Goods goods = new Goods(goodsid,userid);
        return goods;
    }

    @Override
    public Address_sh selectByaddress(String address) {
        String sql ="select addressid,address from tb_address where address = ?";
        List<Map<String,Object>> list = JdbcUtil.selectBySql(sql,address);
        Map<String,Object> map = list.get(0);
        Integer addressid = (Integer) map.get("addressid");
        Address_sh address_sh = new Address_sh(addressid,address);
        return address_sh;
    }

    @Override
    public int saveOne(Order order) {
        String sql = "insert into tb_order values(null,?,?,?,now(),0,?)";
        return JdbcUtil.editBySql(sql,order.getGoodsid(),order.getBuyerid(),order.getSalerid(),order.getAddressid());
    }
}
